<style>
    .good_choose .other .current{
        background: {$_W['system_color']};
        border:1px solid #fff;
    }
</style>
<div class="good_choose_layer" style="display: none;"></div>

<div class="good_choose" style="display: none;z-index:999;">
    <div class="info">
         <div class="left">
             <img id="chooser_img" src="{php echo tomedia($goods['gimg']);}">
         </div>
         <div class="right">
				<!--<div><span style="color:#888;">库存:</span></div>-->
               	<div class="price" id="option_opricee">￥<span id="option_oprice"> {php echo $goods['oprice']}</span></div>
               	<div class="price" id="option_gpricee">￥<span id="option_gprice"> {php echo $goods['gprice']}</span></div>
        		<span id='stockcontainer' class="stock" style="float:left;margin-left:5px;"></span>
         </div>
        <div class="close" onclick="closechoose()"><i class="fa fa-remove-o"></i></div>
    </div>
    <div class="other">
	{if $goods['hasoption']==1}
    	{loop $specs $spec}
   	 	<input type='hidden' name="optionid[]" class='optionid optionid_{$spec['id']}' value="" title="{$spec['title']}">
        <div class="spec" style='float:left;display:block;height:30px;line-height:30px;overflow:hidden;text-overflow:ellipsis;margin-left:10px;padding:0'>{$spec['title']}</div>
            <span style="float:left;margin-left:8px;" class='spec_items options options_{$spec['id']}' specid='{$spec['id']}'>
            {loop $spec['items'] $o}
            <span class="property option option_{$spec['id']}" specid='{$spec['id']}' oid="{$o['id']}" sel='false'>{$o['title']}</span>
            <input type="hidden" name="thumb_{$o['id']}" id="thumb_{$o['id']}" value="{php echo tomedia($o['thumb'])}" />
            {/loop}
            </span>
    	{/loop}
		{/if}
    	{if $goods['one_limit']>1}
    	<span style="float:left;margin-left:8px; " class='spec_items options' id="in">
            <span style="margin-left: 10px;"><font color="#000000" size="2">选择数量</font></span>
			<div class="w-number" id="pro-view-7" style="float: right;">
				<input class="w-number-input" type="text" id="num" name="num" value="1">
				<a class="w-number-btn w-number-btn-plus" id="up">
					+
				</a>
				<a class="w-number-btn w-number-btn-minus" id="down">
					-
				</a>
			</div>
        </span>
        {/if}
    </div>
	<span style="display:none;" id="opstock">{php echo $goods['gnum']}</span>
    <div class="close" onclick="closechoose()"><i class="fa fa-times-circle-o"></i></div>
    <div class="sub" onclick="buy()" id="nowbuy">确认</div>
</div>

<script type="text/javascript">
	function choose(act){
	   	$('.good_choose_layer').fadeIn(200);
			$('.good_choose').fadeIn(200); 
			if (act == 1) {
			
				$("#buytype").val(1);
				$("#option_gpricee").hide();
				$("#option_opricee").show();
			}
			if (act == 2) {
		
				var thisgroupnum = "{$goods['groupnum']}";
				$("#buytype").val(thisgroupnum);
				$("#option_gpricee").show();
				$("#option_opricee").hide();
			}
		
	}
	
	$(function () {
        $('.other-detail .detail-group:last').css("border-bottom", "0");
        $(".option,.optionimg").click(function () {
            var specid = $(this).attr("specid");
            var oid = $(this).attr("oid");
            var thumb = $("#thumb_"+oid).val();
            if(thumb){
            	$("#chooser_img").attr("src",thumb);
            }
            $(".optionid_" + specid).val(oid);
            $(".options_" + specid + "  span").removeClass("current").attr("sel", "false");
            $(this).addClass("current").attr("sel", "true");
            var optionid = "";
            var stock = 0;
            var marketprice = 0;
            var productprice = 0;
            var buytype = $("#buytype").val();
            var ret = option_selected();

            if (ret.no == '') {
                var len = options.length;
                for (var i = 0; i < len; i++) {
                    var o = options[i];
                    var ids = ret.all.join("_");
                    if (o.specs == ids) {
                        optionid = o.id;
                        stock = o.stock;
						
                        marketprice = o.marketprice;
                        productprice = o.productprice;
                        break;
                    }
                }
				$("#opstock").html(stock);
                $("#optionid").val(optionid);
				$("#option_gprice").html(marketprice);
                $("#option_oprice").html(productprice);
                $("#in").show();
                $("#num").val(1);
                if (buytype == 1) {
                	$("#option_gpricee").hide();
                    $("#option_opricee").show();
                } else {
                    $("#option_gpricee").show();
                    $("#option_opricee").hide();
                }
            }
        });
    });
    function buy() {
        if($('.spec_items.options>span.property').length>0) {
            var specLen = $('.spec_items.options');
            var numLen=0;
            for(var j=0;j<specLen.length;j++){
                console.log($(specLen[j]).children('span.property').length);
                if($(specLen[j]).children('span.property').length>0){
                    numLen++;
                }
            }
            var numPro = 0;
            for (var i = 0; i < $('.spec_items.options>span.property').length; i++) {
                if ($($('.spec_items.options>span.property')[i]).attr('sel') == 'true') {
                    numPro++;
                }
            }
            if (numPro != numLen) {
                $.toast("请选择规格!");
                return;
            }
        }

        var ret = option_selected();
        if (ret.no != '') {
            $.toast('请选择'+ret.no + "!");
            return;
        }
        {if $goods['one_limit']>1}
		var inputnum = $("#num").val();
		{else}
		var inputnum = 1;
		{/if}
        var num = Number(document.getElementById("opstock").innerHTML);
        var many_limit = $("#many_limit").val();
        var one_limit = $("#one_limit").val();
        var times = $("#times").val();
        inputnum = parseInt(inputnum);
        num = parseInt(num);
        many_limit = parseInt(many_limit);
        one_limit = parseInt(one_limit);
        times = parseInt(times);
        
	        if(isNaN(inputnum)){
	        	$.toast('数量错误!');
				return false;
	        }else{
	        	if(inputnum<0){
	        		$.toast('数量错误!');
					return false;
	        	}
	        	
	        	if(one_limit>1){
	        		if(inputnum > one_limit){
		        		$.toast('单次购买上限为：'+one_limit);
						return false;
		        	}
	        	}
	        	
	        }
       
		if(inputnum > num){
	        		$.toast('库存不足!');
					return false;
	        	}
		if(many_limit>=1){
	        		if(inputnum+times > many_limit){
		        		$.toast('单人购买总数为：'+many_limit);
						return false;
		        	}
	        	}
        var groupnum = $("#buytype").val();
		var restype=0;
		
		var run_url="{php echo app_url('order/orderconfirm')}" + "&optionid=" + $("#optionid").val() + "&groupnum=" + groupnum+"&num=" + inputnum+"&tuan_id={$tuan_id}&id={$goods['id']}";
	     
		if({$result['statustype']} == 1)
		{
			run_url="{php echo app_url('order/orderconfirm')}" + "&optionid=" + $("#optionid").val() + "&groupnum=" + groupnum+"&num=" + inputnum+"&newtuan=newtua&id={$goods['id']}";
		}
		
        location.href = run_url;
 }   
</script>